package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    /*
        原始分页查询方法 比较繁琐
        @Override
        public PageBean getEmps(Integer page, Integer pageSize) {
            PageBean pageBean = new PageBean();
            pageBean.setTotal(empMapper.count());
            pageBean.setRows(empMapper.getEmps((page - 1) * pageSize, pageSize));
            return pageBean;
        }
    */

    // 使用PageHelper分页查询方法
    @Override
    public PageBean getEmps(Integer page, Integer pageSize) {
        // 1.设置分页参数
        PageHelper.startPage(page, pageSize);

        // 2.执行查询
        List<Emp> empList = empMapper.getEmps();
        Page<Emp> p = (Page<Emp>) empList;

        // 3.封装PageBean对象
        return new PageBean(p.getTotal(), p.getResult());
    }

    @Override
    public void deleteEmps(List<Integer> ids) {
        empMapper.deleteEmps(ids);
    }

    @Override
    public void addEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.addEmp(emp);
    }

    @Override
    public Emp searchEmp(Integer id) {
        return empMapper.searchEmp(id);
    }

    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateEmp(emp);
    }

    @Override
    public Emp login(Emp emp) {
        return empMapper.login(emp);
    }
}
